package com.example.project.dao;

import com.example.project.pojo.*;
import com.example.project.util.DBHelper;


import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;

import java.util.Date;


public class AdminDAO {
    public Admin login(Admin admin) {
        Admin admin1 = new Admin();
        Connection connection = DBHelper.getConnection();
        PreparedStatement ps = null;
        ResultSet rs = null;
        String sql = "select * from admin where admin_name=?";
        try {
            ps = connection.prepareStatement(sql);
            ps.setString(1, admin.getAdminUserName());
            rs = ps.executeQuery();
            String pwd = "";
            while (rs.next()) {
                pwd = rs.getString("admin_password");

            }

            if (admin.getAdminPassWord().equals(pwd) && !admin.getAdminPassWord().isEmpty()) {
                admin1 = admin;
            } else {
                admin1.setAdminUserName("");
            }

        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            DBHelper.close(rs, ps, connection);
        }

        return admin1;
    }


    public int day(Date date) {
        DateFormat dft = new SimpleDateFormat("yyyy-MM-dd");

        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Date timeday = new Date();
        try {
            Date star = dft.parse(format.format(timeday));//开始时间
            Date endDay = dft.parse(String.valueOf(date));//结束时间
            Long starTime = star.getTime();
            Long endTime = endDay.getTime();
            Long num = endTime - starTime;//时间戳相差的毫秒数
//            System.out.println("相差天数为："+);//除以一天的毫秒数
            int rtime = (int) (num / 24 / 60 / 60 / 1000);
            if (rtime > 0) {
                return rtime;
            } else {
                return 0;
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return 0;
    }


}
